package com.seari.groovyrunner.controller;

import com.seari.groovyrunner.bean.DataGridBean;
import com.seari.groovyrunner.bean.GroovyLog;
import com.seari.groovyrunner.service.GroovyLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "groovyLog")
public class GroovyLogController {

    @Value("${groovy.visit.ip}")
    private String visitIP ;

    @Autowired
    public GroovyLogService groovylogService;

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    @ResponseBody
    public DataGridBean<GroovyLog> search(@RequestParam(value = "service")String service,@RequestParam(value = "state")String state, @RequestParam(value = "page")String page, @RequestParam(value = "limit")String limit){
        DataGridBean<GroovyLog> dataDataGridBean = new DataGridBean<>();
        List<GroovyLog> baseLog = groovylogService.getLogListByService(service,state,page,limit);
        int count = groovylogService.getLogCountByName(service);
        for (GroovyLog groovy : baseLog) {
            groovy.setGroovyService(visitIP+"/groovyDynamic/"+groovy.getGroovyService());
        }
        dataDataGridBean.setMsg("success");
        dataDataGridBean.setTotal(count);
        dataDataGridBean.setRows(baseLog);
        dataDataGridBean.setCode(200+"");
        return dataDataGridBean;
    }
}
